package myConnection;

import javaBean.Database;
import javaBean.DatabaseName;
import javaBean.Employee;
import javaBean.News;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public final class DBConn {
    //连接数据库

    public  static SqlSession getSqlSession() {
        String resource = "myXML/mybatis-config.xml";
        InputStream inputStream = null;
        SqlSession session = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            session = sqlSessionFactory.openSession();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return session;
    }

    //校验用户信息
    public static Boolean check(Employee emp){
        SqlSession sqlSession = getSqlSession();
        try {
            //selectOne的第一个参数为Mapper.xml的mapper的namespace+id
            //参数二为映射的需要传入的参数
            Employee employee = sqlSession.selectOne(
                    "MyMapper.selectEmployee_name", emp.getName());
            if (employee!=null && employee.getPassword().equals(emp.getPassword()))
                return true;
            else
                return false;
        } finally {
            free(sqlSession);
        }
    }


    //通过新闻表名获取该数据库的最新新闻
    public static List<News> getNews(String databaseName){
        int num = 1;
        SqlSession sqlSession = getSqlSession();
        //存在一个问题，直接传入String类型的参数时，纯mybatis无法获取参数，所以封装成一个Javabean
        DatabaseName name = new DatabaseName();
        name.setDatabaseName(databaseName);
        List<News> list = sqlSession.selectList("MyMapper.selectByDatabaseName",name);
        //只在首页界面显示最新的8条新闻
        Collections.reverse(list);
        Iterator<News> iterator = list.iterator();
        List<News> temp = new ArrayList<>();
        while (iterator.hasNext() && num<9){
                temp.add(iterator.next());
                num++;
        }
        free(sqlSession);
        return temp;
    }

    //通过新闻表名获取该数据库的全部新闻
    public static List<News> getAllNews(String databaseName){
        SqlSession sqlSession = getSqlSession();
        //存在一个问题，直接传入String类型的参数时，纯mybatis无法获取参数，所以封装成一个Javabean
        DatabaseName name = new DatabaseName();
        name.setDatabaseName(databaseName);
        List<News> list = sqlSession.selectList("MyMapper.selectByDatabaseName",name);
        free(sqlSession);
        return list;
    }



    //通过id获取某一类别的某一条新闻
    public static News getNewsById(String databaseName, int id){
        //设置数据库名和新闻id
        Database database = new Database();
        database.setDatabaseName(databaseName);
        database.setId(id);

        SqlSession sqlSession = getSqlSession();
        News news = sqlSession.selectOne("MyMapper.selectById",database);
        free(sqlSession);
        return news;
    }


    //释放Sqlsession
    public static void free(SqlSession sqlSession){
        sqlSession.close();
    }

    //向数据库添加新闻
    public static void insertNews(News news, String type) {
        SqlSession sqlSession = getSqlSession();
        news.setDatabaseName(type);
        System.out.println(news);
        int result = sqlSession.insert("MyMapper.insertNews",news);
        //提交事务
        sqlSession.commit();
        free(sqlSession);
    }
    //修改新闻信息
    public static void upDataNewsById(News news, String type) {
//        SqlSession sqlSession = getSqlSession();
//        news.setDatabaseName(type);
//        System.out.println(news);
//        int res = sqlSession.update("MyMapper.updateNews",news);
//        sqlSession.commit();
//        free(sqlSession);
        deleteNews(type,news.getId());
        insertNews(news,type);
    }

    //删除新闻
    public static void deleteNews(String databaseName,int id) {
        SqlSession sqlSession = getSqlSession();
        Database database = new Database();
        database.setDatabaseName(databaseName);
        database.setId(id);
        int result = sqlSession.delete("MyMapper.deleteNews",database);
        sqlSession.commit();
        free(sqlSession);
    }
}
